package com.woniuxy.usecredit.controller;/* *
@author author
@create 2021-11-15 12:30
*/

import com.woniuxy.entity.*;
import com.woniuxy.service.ActivitiService;
import com.woniuxy.service.UserService;
import com.woniuxy.service.business.BorrowDemandService;
import com.woniuxy.usecredit.service.EnterpriseCreditUsedService;
import com.woniuxy.usecredit.service.GuaranteeService;
import com.woniuxy.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/guarantee")
public class GuaranteeController {

    @Autowired
    UserService userService;

    @Autowired
    GuaranteeService guaranteeService;

    @Autowired
    EnterpriseCreditUsedService enterpriseCreditUsedService;

    @Autowired
    ActivitiService activitiService;

    @Autowired
    BorrowDemandService borrowDemandService;


    @PostMapping("/")
    public ResponseResult addGuarantee(@RequestBody EnterpriseGuarantee enterpriseGuarantee){

        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(200);

        //通过id查询企业授信情况
        int creditId = enterpriseGuarantee.getEnterpriseCredit().getId();
        EnterpriseCredit credit = activitiService.findCreditById(creditId);
        if(credit==null){
            responseResult.setMessage("未查到该授信记录");
            return responseResult;
        }
        //判断授信是否过期
        if(credit.getEffectiveEnd().before(new Date())){
            responseResult.setMessage("该条授信已过期");
            return responseResult;
        }
        int min=credit.getCreditLine();
        //判断授信余额是否满足转让额度
        List<EnterpriseCreditUsed> creditUseds = enterpriseCreditUsedService.selectCreditUsedByCreditId(creditId);
        if(creditUseds!=null){
            for (EnterpriseCreditUsed ecu:creditUseds) {
                int creditBalance = ecu.getCreditBalance();
                if(min<creditBalance){
                    min=creditBalance;
                }
            }
        }
        int guaranteeUsedAmount = enterpriseGuarantee.getGuaranteeUsedAmount();
        if(min<guaranteeUsedAmount){
            responseResult.setMessage("授信余额不足以转让");
            return responseResult;
        }
        //通过id查询上下游企业
        Integer businessId = enterpriseGuarantee.getGuaranteedEnterprise().getId();
        ResponseResult<BusinessInfo> businessInfoRR = borrowDemandService.getBusinessInfoById(businessId);
        BusinessInfo business = businessInfoRR.getData();
        if(business==null){
            responseResult.setMessage("未查到上下游企业");
            return responseResult;
        }
        //若以上判断均通过，插入转让记录
        int i = guaranteeService.addGuarantee(enterpriseGuarantee);
        if(i==1){
            EnterpriseCreditUsed enterpriseCreditUsed = new EnterpriseCreditUsed();
            enterpriseCreditUsed.setEnterpriseCredit(credit);
            enterpriseCreditUsed.setCreditUsedAmount(guaranteeUsedAmount);
            enterpriseCreditUsed.setCreditBalance(min-guaranteeUsedAmount);
            enterpriseCreditUsed.setCreditUsedDescription("转让用信");
            enterpriseCreditUsed.setUpdateTime(new Date());
            enterpriseCreditUsedService.addCreditUsed(enterpriseCreditUsed);
        }
        responseResult.setMessage("转让成功");
        return  responseResult;

    }

    @GetMapping("/{creditId}")
    public ResponseResult getGuaranteeByCreditId(@PathVariable("creditId") int creditId){
        ResponseResult responseResult = new ResponseResult();
        List<EnterpriseGuarantee> guarantees = guaranteeService.getGuaranteeByCreditId(creditId);
        responseResult.setCode(200);
        responseResult.setMessage("获取转让信息成功");
        responseResult.setData(guarantees);
        return responseResult;
    }

    @GetMapping("/business/{businessName}")
    public ResponseResult getGuaranteeBybusiness(@PathVariable("businessName") String businessName){
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(200);
        ResponseResult<BusinessInfo> businessInforr = borrowDemandService.getBusinessInfoByBusinessName(businessName);
        BusinessInfo business = businessInforr.getData();
        if(business==null){
            responseResult.setMessage("未查到该企业信息");
            return responseResult;
        }
        List<EnterpriseCredit> credits = activitiService.findCreditByEnterpriseId(business.getId());
        if(credits==null&&credits.size()<=0){
            responseResult.setMessage("该企业未授信");
            return responseResult;
        }
        List<EnterpriseGuarantee> allGuarantees = new ArrayList<>();
        for (EnterpriseCredit ec:credits) {
            List<EnterpriseGuarantee> guarantees = guaranteeService.getGuaranteeByCreditId(ec.getId());
            for (EnterpriseGuarantee eg:guarantees) {
                System.out.println(eg);
                int creditid = eg.getEnterpriseCredit().getId();
                EnterpriseCredit credit = activitiService.findCreditById(creditid);
                System.out.println("credit:"+credit);
//                Integer ceid = credit.getCoreEnterprise().getId();
//                int mid = credit.getMerchandiser().getId();
//                ResponseResult<BusinessInfo> businessrr = borrowDemandService.getBusinessInfoById(ceid);
//                credit.setCoreEnterprise(businessrr.getData());
//                User user = userService.getUser(mid);
//                credit.setMerchandiser(user);
                eg.setEnterpriseCredit(credit);
                ResponseResult<BusinessInfo> businessrr = borrowDemandService.getBusinessInfoById(eg.getGuaranteedEnterprise().getId());
                eg.setGuaranteedEnterprise(businessrr.getData());


            }
            allGuarantees.addAll(guarantees);
        }
        responseResult.setMessage("获取转让信息成功");
        responseResult.setData(allGuarantees);
        return responseResult;
    }

    @GetMapping("/all")
    public ResponseResult getGuarantees(){
        ResponseResult responseResult = new ResponseResult();
        List<EnterpriseGuarantee> guarantees = guaranteeService.getAllGuarantee();
        for (EnterpriseGuarantee eg:guarantees) {
            Integer guaranteeid = eg.getGuaranteedEnterprise().getId();
            ResponseResult<BusinessInfo> businessInforr = borrowDemandService.getBusinessInfoById(guaranteeid);
            eg.setGuaranteedEnterprise(businessInforr.getData());
            int creditid = eg.getEnterpriseCredit().getId();
            EnterpriseCredit credit = activitiService.findCreditById(creditid);
 //           System.out.println("credit------++++++:"+credit);
//            Integer ceid = credit.getCoreEnterprise().getId();
//            int mid = credit.getMerchandiser().getId();
//            ResponseResult<BusinessInfo> businessrr = borrowDemandService.getBusinessInfoById(ceid);
//            credit.setCoreEnterprise(businessrr.getData());
//            User user = userService.getUser(mid);
//            credit.setMerchandiser(user);
            eg.setEnterpriseCredit(credit);
        }
        responseResult.setCode(200);
        responseResult.setMessage("获取转让信息成功");
        responseResult.setData(guarantees);
        System.out.println("guarantee---"+guarantees);
        return responseResult;
    }

    @DeleteMapping("/{guaranteeId}")
    public ResponseResult removeGuarantee(@PathVariable("guaranteeId") int guaranteeId){
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(200);
        boolean isRemove = guaranteeService.removeGuarantee(guaranteeId);
        if(isRemove){
            responseResult.setMessage("删除转让记录成功");
        }else {
            responseResult.setMessage("已用信，无法删除");
        }
        return responseResult;
    }
}
